(function () {
  let id;
  let answer;

  const hides = document.querySelectorAll(".hide");
  // 获取密保
  // 第一个按钮
  const oneBtn = document.querySelector("#one");
  const phoneDom = document.querySelector("#phone");
  oneBtn.addEventListener("click", function () {
    const phoneValue = phoneDom.value.trim();
    if (!phoneValue) return alert("请输入手机号");
    axios({
      method: "GET",
      url: "http://124.223.14.236:3001/api/user/findPwd",
      params: {
        phone: phoneValue,
      },
    }).then(function (res) {
      if (!res.data.success) return alert(res.data.msg);
      // 成功后跳到下一个,并赋值上去
      hides[0].classList.remove("activeNow");
      hides[1].classList.add("activeNow");
      const inputs = document.querySelectorAll(".activeNow:nth-child(2) input");
      inputs[0].value = res.data.data.username;
      inputs[1].value = res.data.data.phone;
      inputs[2].value = res.data.data.question;
      // 获取id
      id = res.data.data.id;
    });
  });

  // 第二个按钮
  const twoBtn = document.querySelector("#two");
  twoBtn.addEventListener("click", function () {
    // 获取填写的密保答案
    answer = hides[1]
      .querySelector(".box .input-item:nth-child(4) input")
      .value.trim();
    // 成功后跳到下一个
    hides[1].classList.remove("activeNow");
    hides[2].classList.add("activeNow");
  });

  // 第三个按钮
  const threeBtn = document.querySelector("#three");
  threeBtn.addEventListener("click", function () {
    const password = hides[2].querySelector(".box input").value.trim();
    axios({
      method: "POST",
      url: "http://124.223.14.236:3001/api/user/resetPwd",
      data: {
        id: `${id}`,
        password,
        answer,
      },
    }).then(function (res) {
      if (!res.data.success) return alert(res.data.msg);
      alert("修改密码成功！");
      window.location.href = "../../loginBaseCode/login.html";
    });
  });

  // 回到上一个
  twoBtn.previousElementSibling.addEventListener("click", function () {
    hides[1].classList.remove("activeNow");
    hides[0].classList.add("activeNow");
  });

  threeBtn.previousElementSibling.addEventListener("click", function () {
    hides[2].classList.remove("activeNow");
    hides[1].classList.add("activeNow");
  });
})();
